#include "stu.h"

void menu()
{
    printf("------------------------------\n");
    printf("----1.添加------2.删除--------\n");
    printf("----3.修改------4.查询--------\n");
    printf("----5.排序------6.展示--------\n");
    printf("----------0.退出--------------\n");
    printf("------------------------------\n");
}

int add_stu(cla_t* cla)
{
    int ret = 0;
    //判断是否已满
    if(++(cla->num) > MAXSTU - 1)   //已经30个人了
    {
        printf("班级内的学生满了，添加失败!\n");
        (cla->num)--;
        return -1;
    }
retry:
     printf("input (name,sex,score) > ");
     //scanf的返回值是输入成功的项目的个数
     ret = scanf("%s %c %d",cla->st[cla->num].name, &cla->st[cla->num].sex, &cla->st[cla->num].score);
     //检查输入是否有误
    if(ret != 3)
    {
        printf("输入有误，请重新输入！\n");
        while(getchar() != '\n');
        goto retry;
    }
    //缩小检查范围，使检查输入更为严格
    if(cla->st[cla->num].sex != 'm' && cla->st[cla->num].sex != 'w')
    {
        printf("性别输入有误！\n");
        while(getchar() != '\n');
        goto retry;
    }
    if(cla->st[cla->num].score < 0 && cla->st[cla->num].score > 100)
    {
        printf("成绩输入有误！\n");
        while(getchar() != '\n');
        goto retry;
    }
    printf("添加成功！\n");

    return 0;
}

int del_stu(cla_t* cla)
{
    char name[20] = { 0 };
    int i, j = 0;
    printf("请输入要删除的人的姓名 >");
    scanf("%s", name);
    while(getchar()!='\n');
    for(i = 0, j = 0; i <= cla->num; i++)
    {
        if(strcmp(name, cla->st[i].name) != 0)
        {
            cla->st[j++] = cla->st[i];
        }
    }
    cla->num = j - 1;
    return i - j;
}

unsigned int che_stu(cla_t* cla)
{
    char name[20] = { 0 };
    int i = 0;
    int ret = 0;
    int num = 0;
    printf("请输入要查询的姓名 >");
    scanf("%s", name);
    while(getchar() != '\n');
    for(i = 0; i <= cla->num; i++)
    {
        if(strcmp(name, cla->st[i].name) == 0)
        {
            printf("id=%d,name = %-15s,sex = %c,score = %d\n", ret++,
            cla->st[i].name, cla->st[i].sex, cla->st[i].score);
            num = num | (1<<i); //记录每一个查找到人的位置
        }
    }
    return num;
}
int get_num_bit(unsigned int num)
{
    int i = 0;
    int ret = 0;
    for(i = 0; i < MAXSTU; i++)
    {
        if(num>>i & 0x1)
        {
            ret++;
        }
    }
    return ret;
}

int mod_stu(cla_t* cla)
{
    int num = 0;
    int id = 0;
    int i = 0;
    int ret = 0;
    //1.查询要删除的学生
    num = che_stu(cla);
    if(0 == num)
    {
        printf("不存在这个学生！\n");
        return -1;
    }
    //2.修改学生信息
    printf("请输入想要修改的学生的ID > ");
    scanf("%d", &id);
    while(getchar()!='\n');

    for(i = 0; i < MAXSTU; i++)
    {
        if(num>>i & 0x1)    //找到符合条件的学生
        {
            if(ret == id)   //找到要修改的同名学生后，直接跳出循环进行修改
            {
                break;
            }
            ret++;  //记录同名学生个数
        }
    }
retry:
    printf("input (name,sex,score) > ");
    //scanf的返回值是输入成功的项目的个数
    ret = scanf("%s %c %d", cla->st[i].name, &cla->st[i].sex, &cla->st[i].score);
    if(ret != 3)
    {
        printf("input error,try again\n");
        while(getchar()!='\n'); //吃掉垃圾字符
        goto retry;
    }

    return 0;
}
void sort_student(cla_t *cla)
{
    int i,j;
    stu_t stu;
    for(i=0; i<cla->num; i++){
        for(j=0; j<cla->num-i; j++){
            if(cla->st[j].score > (cla->st[j+1].score)){
                stu = cla->st[j];
                cla->st[j] = cla->st[j+1];
                cla->st[j+1] = stu;
            }
        }
    }

}
void show_student(cla_t* cla)
{
    int i = 0;
    for (i = 0; i <= cla->num; i++)
    {
        printf("name = %-15s,sex = %c,score = %d\n", cla->st[i].name, cla->st[i].sex, cla->st[i].score);
    }
}